package com.palpiteiro.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.palpiteiro.bean.*;

public class EmpresaDAO extends IDAO{

	public ResultSet selectAll() throws SQLException {

		String sql = "";
		
		sql = "select id_empresa,sigla,nome"
				+ " from \"Empresa\".\"Empresa\" order by sigla";
		
		PreparedStatement stmt = getConn().prepareStatement(sql);
		
		ResultSet rs = stmt.executeQuery();
		
		return rs;
	}
	
	@Override
	public ResultSet select(IBean bean) throws SQLException {

		Empresa empresa = (Empresa) bean;
		String sql = "";
		
		sql = "select id_empresa,sigla,nome"
				+ " from \"Empresa\".\"Empresa\""
				+ " where id_empresa = ?;";
		
		PreparedStatement stmt = getConn().prepareStatement(sql);
		stmt.setInt(1, empresa.getId_empresa());
		
		ResultSet rs = stmt.executeQuery();
		
		return rs;
	}

	@Override
	public void insert(IBean bean) throws SQLException {

		Empresa empresa = (Empresa) bean;
		String sql = "";
		
		sql = "insert into \"Empresa\".\"Empresa\" " + 
						"(sigla,nome)" + 
						" values(?,?)";

		PreparedStatement stmt = getConn().prepareStatement(sql);
		
		stmt.setString(1, empresa.getSigla());
		stmt.setString(2, empresa.getNome());

		stmt.executeUpdate();
		
		stmt.close();
		closeConn();
	}

	@Override
	public void update(IBean bean) throws SQLException {

		Empresa empresa = (Empresa) bean;
		String sql = "";
		
		sql = "update \"Empresa\".\"Empresa\" set " + 
				"sigla=?,nome=? where id_empresa=?";

		PreparedStatement stmt = getConn().prepareStatement(sql);
		
		stmt.setString(1, empresa.getSigla());
		stmt.setString(2, empresa.getNome());
		stmt.setInt(3, empresa.getId_empresa());

		stmt.executeUpdate();
		
		stmt.close();
		closeConn();
	}

	
}
